/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.peng.rest;

import me.peng.annotation.Log;
import me.peng.domain.RecyclerFind;
import me.peng.domain.config.SysResult;
import me.peng.domain.mapper.FindMapper;
import me.peng.service.RecyclerFindService;
import me.peng.service.dto.RecyclerFindQueryCriteria;
import me.peng.service.impl.FileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.peng.annotation.rest.AnonymousGetMapping;
import me.peng.annotation.rest.AnonymousPutMapping;
/**
 * @website https://el-admin.vip
 * @author pengjianye
 * @date 2021-10-02
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "api/find管理")
@RequestMapping("/api/recyclerFind")
public class RecyclerFindController {

    private final RecyclerFindService recyclerFindService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('recyclerFind:list')")
    public void download(HttpServletResponse response, RecyclerFindQueryCriteria criteria) throws IOException {
        recyclerFindService.download(recyclerFindService.queryAll(criteria), response);
    }

    @AnonymousGetMapping
    @Log("查询api/find")
    @ApiOperation("查询api/find")
//  @PreAuthorize("@el.check('recyclerFind:list')")
    public ResponseEntity<Object> query(RecyclerFindQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(recyclerFindService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增api/find")
    @ApiOperation("新增api/find")
    @PreAuthorize("@el.check('recyclerFind:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RecyclerFind resources){
        //字符串有值
        if (FileController.list.length()!=0){
            resources.setImages(FileController.list);
            FileController.list="";
        }
        return new ResponseEntity<>(recyclerFindService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改api/find")
    @ApiOperation("修改api/find")
    @PreAuthorize("@el.check('recyclerFind:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RecyclerFind resources){
        //用户点击了修按钮,先删除原先的图片文件
        if (resources.getImages()!=null){
            //分割
            String[] strings=resources.getImages().split(",");
            for (String s:strings){
                //删除
                FileServiceImpl.deleteFileData(s);
            }
        }
        if (FileController.list.length()!=0){
            //从上传文件中取出数据
            resources.setImages(FileController.list);
            FileController.list="";
        }
        recyclerFindService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除api/find")
    @ApiOperation("删除api/find")
    @PreAuthorize("@el.check('recyclerFind:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        recyclerFindService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Autowired
    RecyclerFindService recyclerFindService2;

    @AnonymousPutMapping("/putFindInfo")
    public SysResult putFindInfo(@RequestBody  RecyclerFind recyclerFind){
        return recyclerFindService2.putFindInfo(recyclerFind);
    }

}
